﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CourierManager;
using System.IO;

namespace TeamWPF
{
    /// <summary>
    /// Interaction logic for EmployeeWindow.xaml
    /// </summary>
    public partial class EmployeeWindow : Window
    {
        public EmployeeWindow()
        {
            InitializeComponent();
        }

        private void ProcessEmployeeType(object sender, RoutedEventArgs e)
        {
            if (sender as MenuItem == CustomerService)
            {
                DefaultType.Header = CustomerService.Header;
                DefaultType.Background = Brushes.Azure;
            }
  
            else if (sender as MenuItem == Driver)
            {
                DefaultType.Header = Driver.Header;
                DefaultType.Background = Brushes.Azure;
            }

            else if (sender as MenuItem == Accountant)
            {
                DefaultType.Header = Accountant.Header;
                DefaultType.Background = Brushes.Azure;
            }

            else
            {
                DefaultType.Header = Manager.Header;
                DefaultType.Background = Brushes.Azure;
            }
        }
        
        private void ProcessEmployeeData(object sender, RoutedEventArgs e)
        {
            string employeeType = this.DefaultType.Header.ToString();

            string firstName = this.NewEmployeeFirstNameInput.Text;
            try
            {
                ValidatorName.PersonalFirstNameValidator(firstName);
            }
            catch (InvalidNameException)
            {
                ErrorWindow wrongFirstNameWindow = new ErrorWindow();
                wrongFirstNameWindow.ErrorText.Text = "Enter a valid first name!";
                wrongFirstNameWindow.ShowDialog();
                return;
            }

            string lastName = this.NewEmployeeLastNameInput.Text;
            try
            {
                ValidatorName.PersonalLastNameValidator(lastName);
            }
            catch (InvalidNameException)
            {
                ErrorWindow wrongLastNameWindow = new ErrorWindow();
                wrongLastNameWindow.ErrorText.Text = "Enter a valid last name!";
                wrongLastNameWindow.ShowDialog();
                return;
            }

            string address = null;
            if (this.NewEmployeeAddressInput.Text != string.Empty)
            {
                address = this.NewEmployeeAddressInput.Text;
            }
            else
            {
                ErrorWindow emptyAddressWindow = new ErrorWindow();
                emptyAddressWindow.ErrorText.Text = "Enter a valid adrreess!";
                emptyAddressWindow.ShowDialog();
                return;
            }          

            string idStr = this.NewEmployeeIDInput.Text;
            try
            {
                ValidatorIDNumber.ValidatePersonalIDNumber(idStr);

            }
            catch (InvalidIDNumberException)
            {
                ErrorWindow wrongIDWindow = new ErrorWindow();
                wrongIDWindow.ErrorText.Text = "Enter a valid ID!";
                wrongIDWindow.ShowDialog();
                return;
            }
            ulong id = ulong.Parse(idStr);

            string phoneStr = this.NewEmployeePhoneInput.Text;
            try
            {
                ValidatorPhone.ValidatePhoneNumberFromString(phoneStr);
            }
            catch (InvalidPhoneNumberException)
            {
                ErrorWindow wrongPhoneWindow = new ErrorWindow();
                wrongPhoneWindow.ErrorText.Text = "Enter a valid phone!";
                wrongPhoneWindow.ShowDialog();
                return;
            }
            ulong phone = ulong.Parse(phoneStr);

            decimal salary = 0;
            try
            {
                salary = decimal.Parse(this.NewEmployeeSalaryInput.Text);
            }
            catch (FormatException)
            {
                ErrorWindow wrongSalaryWindow = new ErrorWindow();
                wrongSalaryWindow.ErrorText.Text = "Enter a valid salary!";
                wrongSalaryWindow.ShowDialog();
                return;
            }

            switch (employeeType)
            {
                case "Customer Service":
                    CourierManager.CustomerServiceEmployee newCustSerEmployee = new CustomerServiceEmployee(firstName, lastName, address, id, phone, salary);
                    newCustSerEmployee.SaveEmployeeData();
                    break;
                case "Driver":
                    Driver newDriver = new Driver(firstName, lastName, address, id, phone, salary);
                    newDriver.SaveEmployeeData();
                    break;
                case "Accountant":
                    Accountant newAccountant = new Accountant(firstName, lastName, address, id, phone, salary);
                    newAccountant.SaveEmployeeData();
                    break;
                case "Manager":
                    Manager newManager = new Manager(firstName, lastName, address, id, phone, salary);
                    newManager.SaveEmployeeData();
                    break;
                default:
                    ErrorWindow emptyInputFieldsWindow = new ErrorWindow();
                    emptyInputFieldsWindow.ErrorText.Text = "Empty type field!";
                    emptyInputFieldsWindow.Show();
                    return;
            }
            Close();
        }    
    }
}
